import store from '@/store/index.js'
/**
 *
 * @param {array} orginRoutes
 * @returns {array}
 * 将路由处理成能用的
 */
export function loopRoute(data,parentPath = '') {
  console.log(data)
  let routes = []
  data.forEach(val => {
    let route = {}
    route.path =parentPath + '/'+  val.path
    if (val.props) {
      route.props = val.props
    }
    if (val.name) {
      route.name = val.name
    }
    if (val.componet) {
      
      if (val.componet === 'Layout') {
        route.component = () => import('@/pages/layout')
      } else if(val.componet) {
        const url = val.componet
        route.component = () => import(`${url}`)
      }
    }

    if (val.redirect && val.redirect != 'NULL') {
      route.redirect = val.redirect
    }
    route.meta = val.meta
    if (val.child && val.child.length !== 0) {
      route.children = loopRoute(val.child,route.path)
    }
    routes.push(route)
  })
  return routes
}

export function getSubRoutes() {//获取所有子路由，返回一个数组
  let result = []
  for (let item of store.getters.routes) {
    if (item.children)
      for (let subItem of item.children) {
        result.push(subItem.name)
      }
  }
  return result
}
